// import com.alibaba.druid.sql.ast.SQLName_ptr ;
// import com.alibaba.druid.sql.ast.SQLSubPartitionBy;
// import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject;
// import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

#include "MySqlSubPartitionByKey.h"
#include "../../../../../sql/ast/SQLName.h"
#include "../../../../../sql/ast/SQLSubPartitionBy.h"
#include "../../../../../sql/visitor/SQLASTVisitor.h"
#include "../../../../../sql/dialect/mysql/visitor/MySqlASTVisitor.h"
#include "../../../../../utils/instanceof.h"
#include "../../../../../Exception/IllegalArgumentException.h"
#include "../../../../../utils/listTran.h"
#include "../../../../../utils/log.h"

MySqlSubPartitionByKey::MySqlSubPartitionByKey()
{
  algorithm = 2;
  columns = std::make_shared<std::list<SQLName_ptr>>();
}

// @Override
void MySqlSubPartitionByKey::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, MySqlASTVisitor>(visitor.get()))
  {
    MySqlASTVisitor_ptr tmp = std::dynamic_pointer_cast<MySqlASTVisitor>(visitor);
    accept0(tmp);
  }
  else
  {
    throw new IllegalArgumentException(make_string_ptr("not support visitor type : " + *visitor->getClass()));
  }
}

// @Override
void MySqlSubPartitionByKey::accept0(MySqlASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(MySqlSubPartitionByKey)))
  {
    SQLObject_list_ptr  tmp1 = ListTran::tran_class<SQLName, SQLObject>(columns);
    acceptChild(visitor, tmp1);
    acceptChild(visitor, subPartitionsCount);
  }
  visitor->endVisit(SharedObject(MySqlSubPartitionByKey));
}

SQLName_list_ptr MySqlSubPartitionByKey::getColumns()
{
  return columns;
}

void MySqlSubPartitionByKey::addColumn(SQLName_ptr column)
{
  if (column != nullptr)
  {
    column->setParent(SharedObject(MySqlSubPartitionByKey));
  }
  this->columns->push_back(column);
}

void MySqlSubPartitionByKey::cloneTo(MySqlSubPartitionByKey_ptr x)
{
  SQLSubPartitionBy::cloneTo(x);
  for (SQLName_ptr column : *columns)
  {
    SQLName_ptr c2 = std::dynamic_pointer_cast<SQLName>(column->clone());
    c2->setParent(x);
    x->columns->push_back(c2);
  }
  x->algorithm = algorithm;
}

SQLObject_ptr MySqlSubPartitionByKey::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<MySqlSubPartitionByKey>(new MySqlSubPartitionByKey());
  std::shared_ptr<MySqlSubPartitionByKey> x = std::dynamic_pointer_cast<MySqlSubPartitionByKey>(clone_tmp);
  // MySqlSubPartitionByKey *x = new MySqlSubPartitionByKey();
  cloneTo(x);
  return x;
}

int MySqlSubPartitionByKey::getAlgorithm()
{
  return algorithm;
}

void MySqlSubPartitionByKey::setAlgorithm(int algorithm)
{
  this->algorithm = algorithm;
}

BOOL_ptr MySqlSubPartitionByKey::isPartitionByColumn(long columnNameHashCode64)
{
  for (SQLName_ptr column : *columns)
  {
    if (column->nameHashCode64() == columnNameHashCode64)
    {
      return BOOL::TRUE;
    }
  }
  return BOOL::FALSE;
}
